<?php
/**
 * Copyright (c) 2008-2011 Submit Consulting. Lima-Peru. All Right Reserved.
 * @author Ing. Angel Sullon M. (asullom@hotmail.com)
 * Created: 21.SEP.2000
 * Last Modify: 11.AGO.2011
 * Description of AvanceDiarioData: CRUD
 */
class AvanceDiarioData {
	//	ItemId,
	//	DiaId,
	//	EsfuerzoRestante
	public function create(AvanceDiario $nuevo) {
		$tran= new ConnectDB();
		$tran->getConnection();
		$r=0;
		try{
			$tran->begin();
			$qry=" INSERT INTO AvanceDiario
            	(ItemId,DiaId,EsfuerzoRestante)
                values(?,?,?) ";
			$pre = new PrepareQuery($qry);
			$pre->set($nuevo->getItemRef()->getItemId());
			$pre->set($nuevo->getDiaRef()->getDiaId());
			$pre->set($nuevo->getEsfuerzoRestante());
			mysql_query($pre->getQuery()) or trigger_error("<p class=error>".mysql_error()." table AvanceDiario", E_USER_ERROR. "</p>");;
			$r=mysql_affected_rows();
			$tran->commit();
		}catch(Exception $e){
			$tran->rollback();
			throw new GlobalException("AvanceDiarioData.create ".$e->getMessage());
		}
		$tran->close();
		return $r;
	}

	public function listByFilter($filter) {//CRUD create, reader, update, delete
		$tran= new ConnectDb();
		$tran->getConnection();
		$li=array();
		try{
			$qry=" SELECT u.ItemId,u.DiaId,u.EsfuerzoRestante
                FROM AvanceDiario u
                WHERE  UPPER(u.ItemId) LIKE UPPER(?)
                order by u.DiaId";
			$pre = new PrepareQuery($qry);
			$pre->set($filter."%");
			$rs=mysql_query($pre->getQuery()) or trigger_error("<p class=error>".mysql_error()." table AvanceDiario", E_USER_ERROR. "</p>");
			$u=0;
			while( $row=mysql_fetch_array($rs) ) {
				$d=new AvanceDiario();
				$d->getItemRef()->setItemId($row["ItemId"]);
				$d->getDiaRef()->setDiaId($row["DiaId"]);
				$d->setEsfuerzoRestante($row["EsfuerzoRestante"]);
				$li[$u++]=$d;
			}
			mysql_free_result($rs);
		}catch(Exception $e){
			throw new GlobalException("AvanceDiarioData.listByFilter ".$e->getMessage());
		}
		$tran->close();
		return $li;
	}

	public function findByPk($itemId, $diaId) {
		$tran= new ConnectDB();
		$tran->getConnection();
		$d=new AvanceDiario();
		try{
			$qry=" SELECT u.ItemId,u.DiaId,u.EsfuerzoRestante
                FROM AvanceDiario u
                WHERE u.ItemId = ? AND u.DiaId = ?";
			$pre = new PrepareQuery($qry);
			$pre->set($itemId);
			$pre->set($diaId);
			$rs=mysql_query($pre->getQuery()) or trigger_error("<p class=error>".mysql_error()." table AvanceDiario", E_USER_ERROR. "</p>");;
			if( $row=mysql_fetch_array($rs) ) {
				$d->getItemRef()->setItemId($row["ItemId"]);
				$d->getDiaRef()->setDiaId($row["DiaId"]);
				$d->setEsfuerzoRestante($row["EsfuerzoRestante"]);
			}
			mysql_free_result($rs);
		}catch(Exception $e){
			throw new GlobalException("AvanceDiarioData.findByPk ".$e->getMessage());
		}
		$tran->close();
		return $d;
	}

	public function update(AvanceDiario $dato) {
		$tran= new ConnectDB();
		$tran->getConnection();
		$r=0;
		try{
			$qry=" UPDATE AvanceDiario SET
                EsfuerzoRestante = ?
                WHERE ItemId = ? AND DiaId = ?";
			$pre = new PrepareQuery($qry);
			$pre->set($dato->getEsfuerzoRestante());
			$pre->set($dato->getItemRef()->getItemId());
			$pre->set($dato->getDiaRef()->getDiaId());
			mysql_query($pre->getQuery()) or trigger_error("<p class=error>".mysql_error()." table AvanceDiario", E_USER_ERROR. "</p>");
			$r=mysql_affected_rows();
		}catch(Exception $e){
			throw new GlobalException("AvanceDiarioData.update ".$e->getMessage());
		}
		$tran->close();
		return $r;
	}

	public function delete($itemId, $diaId) {
		$tran= new ConnectDB();
		$tran->getConnection();
		$r=0;
		try{
			$qry=" DELETE FROM AvanceDiario WHERE ItemId = ? AND DiaId = ? ";
			$pre = new PrepareQuery($qry);
			$pre->set($itemId);
			$pre->set($diaId);
			mysql_query($pre->getQuery()) or trigger_error("<p class=error>".mysql_error()." table AvanceDiario", E_USER_ERROR. "</p>");;
			$r=mysql_affected_rows();
		}catch(Exception $e){
			throw new GlobalException("AvanceDiarioData.delete ".$e->getMessage());
		}
		$tran->close();
		return $r;
	}

}
?>